//package cn.lijiancn01.demo.business.tenant.biz;
//
//import cn.hutool.core.bean.BeanUtil;
//import cn.lijiancn01.demo.business.tenant.model.SaveTenantDTO;
//import cn.lijiancn01.demo.business.tenant.model.TenantDO;
//import cn.lijiancn01.demo.business.tenant.model.TenantQuery;
//import cn.lijiancn01.demo.framework.util.SqlUtil;
//import org.apache.commons.collections4.CollectionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.jdbc.core.BeanPropertyRowMapper;
//import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
//import org.springframework.stereotype.Component;
//
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.Objects;
//
//@Component
//public class TenantBiz {
//
//    @Autowired
//    private NamedParameterJdbcTemplate jdbcTemplate;
//
//    public Integer count(TenantQuery req) {
//        String sql = "select count(1) from tenant where is_delete = 'N'";
//        sql += addWhere(req);
//        Map<String, Object> param = param(req);
//        return jdbcTemplate.queryForObject(sql, param, Integer.class);
//    }
//
//    public List<TenantDO> list(TenantQuery req) {
//        String sql = "select * from tenant where is_delete = 'N'";
//        sql += addWhere(req);
//        sql += " order by last_modified_time desc, pkid desc";
//        sql += SqlUtil.addLimitSql();
//        Map<String, Object> param = param(req);
//        return jdbcTemplate.query(sql, param, new BeanPropertyRowMapper<>(TenantDO.class));
//    }
//
//    public TenantDO findByCode(String tenantCode) {
//        String sql = "select * from tenant where is_delete = 'N' and tenant_code = :tenantCode";
//        Map<String, Object> param = new HashMap() {{
//            put("tenantCode", tenantCode);
//        }};
//        List<TenantDO> list = jdbcTemplate.query(sql, param, new BeanPropertyRowMapper<>(TenantDO.class));
//        return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
//    }
//
//    public int updateByCode(SaveTenantDTO saveTenantDTO) {
//        String sql = """
//                update tenant set
//                tenant_name = :tenantName,
//                tenant_type = :tenantType,
//                tenant_status = :tenantStatus,
//                entry_date = :entryDate,
//                expire_date = :expireDate,
//                last_modified_time = now()
//                where is_delete = 'N' and tenant_code = :tenantCode
//                """;
//        Map<String, Object> param = BeanUtil.beanToMap(saveTenantDTO);
//        return jdbcTemplate.update(sql, param);
//    }
//
//    public int add(SaveTenantDTO saveTenantDTO) {
//        String sql = """
//                insert into tenant(tenant_code, tenant_name, tenant_type, tenant_status, entry_date, expire_date,
//                    is_delete, added_time, last_modified_time)
//                values(:tenantCode, :tenantName, :tenantType, :tenantStatus, :entryDate, :expireDate,
//                    'N', now(), now())
//                """;
//        Map<String, Object> param = BeanUtil.beanToMap(saveTenantDTO);
//        return jdbcTemplate.update(sql, param);
//    }
//
//    public int delete(String tenantCode) {
//        String sql = """
//                update tenant set
//                is_delete = 'Y',
//                last_modified_time = now()
//                where tenant_code = :tenantCode
//                """;
//        Map<String, Object> param = new HashMap() {{
//            put("tenantCode", tenantCode);
//        }};
//        return jdbcTemplate.update(sql, param);
//    }
//
//    public int batchDelete(List<String> tenantCodes) {
//        String sql = """
//                update tenant set
//                is_delete = 'Y',
//                last_modified_time = now()
//                where tenant_code in (:tenantCodes)
//                """;
//        Map<String, Object> param = new HashMap() {{
//            put("tenantCodes", tenantCodes);
//        }};
//        return jdbcTemplate.update(sql, param);
//    }
//
//    private Map<String, Object> param(TenantQuery req) {
//        return new HashMap() {{
//            put("page", (req.getPage() - 1) * req.getPerPage());
//            put("perPage", req.getPerPage());
//            put("tenantCode", req.getTenantCode());
//            put("tenantName", req.getTenantName());
//            put("tenantStatus", req.getTenantStatus());
//            put("entryDate", req.getEntryDate());
//            put("expireDate", Objects.nonNull(req.getExpireDate()) ? req.getExpireDate().plusDays(1) : null);
//        }};
//    }
//
//    /**
//     * 组装查询条件
//     *
//     * @param req
//     * @return
//     */
//    private String addWhere(TenantQuery req) {
//        StringBuffer resultSql = new StringBuffer();
//        if (StringUtils.isNotBlank(req.getTenantCode())) {
//            resultSql.append(" and tenant_code = :tenantCode");
//        }
//        if (StringUtils.isNotBlank(req.getTenantName())) {
//            resultSql.append(" and tenant_name = :tenantName");
//        }
//        if (StringUtils.isNotBlank(req.getTenantStatus())) {
//            resultSql.append(" and tenant_status = :tenantStatus");
//        }
//        if (Objects.nonNull(req.getEntryDate())) {
//            resultSql.append(" and entry_date >= :entryDate");
//        }
//        if (Objects.nonNull(req.getExpireDate())) {
//            resultSql.append(" and expire_date <= :expireDate");
//        }
//        return resultSql.toString();
//    }
//}
